﻿Imports System.ComponentModel
Imports System.Collections.Specialized

Public Class MainWindowVM
    Inherits KeyedViewModelBase

    Public Sub New()
        mImportRawsCommand = New Command(AddressOf ImportRaws)
        mNotImplementedCommand = New Command(AddressOf NotImplemented, Function() False)
        mExitCommand = New Command(AddressOf ExitApp)
        mAddObjectCommand = New Command(AddressOf AddObject_Execute)

        ObjectEditor = New ObjectEditorVm

        Dim t As New Task(Sub()
                              TokenDictionary.Instance.Load("Data\TokenDefinitions.xml")
                          End Sub)
        t.Start()
    End Sub

#Region "Commands"
    Private mIsAddObjectEnabled As Boolean
    Private mImportRawsCommand As ICommand
    Public ReadOnly Property ImportRawsCommand As ICommand
        Get
            Return mImportRawsCommand
        End Get
    End Property

    Private mExitCommand As ICommand
    Public ReadOnly Property ExitCommand As ICommand
        Get
            Return mExitCommand 'Command.SimpleClosedCommand
        End Get
    End Property

    Private mEditInNewWindowCommand As ICommand
    Public ReadOnly Property EditInNewWindowCommand As ICommand
        Get
            Return mEditInNewWindowCommand
        End Get
    End Property

    Private mNotImplementedCommand As ICommand
    Public ReadOnly Property NotImplementedCommand As ICommand
        Get
            Return mNotImplementedCommand
        End Get
    End Property

    Private mAddObjectCommand As ICommand
    Public ReadOnly Property AddObjectCommand As ICommand
        Get
            Return mAddObjectCommand
        End Get
    End Property
#End Region

#Region "Command Handlers"
    Private Sub NotImplemented(parameter As Object)

    End Sub

    Private Sub AddObject_Execute(parameter As Object)
        If Library IsNot Nothing Then
            Dim wnd As New AddObjectView
            If CurrentNode IsNot Nothing Then
                wnd.DataContext = New AddObjectVm(Library.RawLibrary, New TokenLib.TypeCode(CurrentNode.ObjectType, CurrentNode.ObjectSubType))
            Else
                wnd.DataContext = New AddObjectVm(Library.RawLibrary, New TokenLib.TypeCode(ObjectType.CREATURE, ObjectSubType.CREATURE))
            End If
            wnd.ShowDialog()
        End If
    End Sub

    Private Sub ExitApp(param As Object)
        SendMessage(New Messages.MainWindow.Close)
    End Sub

    Private Sub ImportRaws(parameter As Object)
        Dim message As New Messages.MainWindow.GetRawDirectory
        message.RecentDirectories = New List(Of String)
        SendMessage(message)
        If message.Handled AndAlso message.RawDirectoryPath IsNot Nothing Then
            Dim rawLib As New RawLibrary
            rawLib.ParseRawDirectory(message.RawDirectoryPath)
            Library = New RawLibraryVm(rawLib)
            RaisePropertyChanged("ObjectCount")
        End If
    End Sub
#End Region

    Private mLibrary As RawLibraryVm
    Public Property Library As RawLibraryVm
        Get
            Return mLibrary
        End Get
        Private Set(value As RawLibraryVm)
            mLibrary = value
            RaisePropertyChanged("Library")
            IsAddObjectEnabled = mLibrary IsNot Nothing
        End Set
    End Property

    Private mCurrentObject As RawObject
    Public Property CurrentObject As RawObject
        Get
            Return mCurrentObject
        End Get
        Set(value As RawObject)
            mCurrentObject = value
            If mCurrentObject IsNot Nothing Then
                ObjectEditor.CurrentObject = mCurrentObject
                RaisePropertyChanged("CurrentObject")
                StatusText = GetObjectStatusText(mCurrentObject)
            End If
        End Set
    End Property

    Private mCurrentNode As NodeVm
    Public Property CurrentNode As NodeVm
        Get
            Return mCurrentNode
        End Get
        Set(value As NodeVm)
            If mCurrentNode IsNot value Then
                mCurrentNode = value
                RaisePropertyChanged("CurrentNode")
            End If
        End Set
    End Property


    Public Property IsAddObjectEnabled As Boolean
        Get
            Return mIsAddObjectEnabled
        End Get
        Set(ByVal Value As Boolean)
            If (mIsAddObjectEnabled = Value) Then Return
            mIsAddObjectEnabled = Value
            RaisePropertyChanged("IsAddObjectEnabled")
        End Set
    End Property


    Private mObjectEditor As ObjectEditorVm
    Public Property ObjectEditor As ObjectEditorVm
        Get
            Return mObjectEditor
        End Get
        Set(value As ObjectEditorVm)
            mObjectEditor = value
            RaisePropertyChanged("ObjectEditor")
        End Set
    End Property

    Public ReadOnly Property ObjectCount As Integer
        Get
            If mLibrary IsNot Nothing Then
                Return mLibrary.RawLibrary.GetAllObjects.Count
            Else
                Return 0
            End If
        End Get
    End Property

    Private mStatusText As String = "Welcome to Raw Explorer!"
    Public Property StatusText As String
        Get
            Return mStatusText
        End Get
        Set(value As String)
            If value <> mStatusText Then
                mStatusText = value
                RaisePropertyChanged("StatusText")
            End If
        End Set
    End Property

    Private Function GetObjectStatusText(obj As RawObject) As String
        If obj IsNot Nothing Then
            Return String.Format("""{0}"" from {1}", obj.ObjectId, obj.Source.Path)
        Else
            Return ""
        End If
    End Function

End Class


